package com.hospital.hospitalsystem.modules.hospital.controller;

import com.hospital.hospitalsystem.base.BaseResponse;
import com.hospital.hospitalsystem.base.PageData;
import com.hospital.hospitalsystem.config.TokenVerify;
import com.hospital.hospitalsystem.modules.hospital.biz.HospitalBiz;
import com.hospital.hospitalsystem.modules.hospital.dto.*;
import com.hospital.hospitalsystem.utils.LoginUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author qiancheng@cqxyy.net
 * @date 2023/2/22
 */
@RestController
@RequestMapping("/hospital")
@Api(tags = "医院管理")
@Validated
public class HospitalController {

    @Autowired
    private HospitalBiz hospitalBiz;

    @Autowired
    private LoginUtils loginUtils;

    @ApiOperation(value = "添加部门配置")
    @RequestMapping(path = "/department/add",method = RequestMethod.PUT)
    @TokenVerify(type = "hospital")
    public BaseResponse departmentAdd(@RequestBody AddDepartmentReq req){
        req.setHospitalUuid(loginUtils.getUser().getUserUuid());
        hospitalBiz.departmentAdd(req);
        return BaseResponse.success();
    };

    @ApiOperation(value = "查看部门配置")
    @RequestMapping(path = "/department/list",method = RequestMethod.POST)
    @TokenVerify(type = "hospital,user")
    public BaseResponse<PageData<DepartmentRsp>> departmentList(@RequestBody DepartmentListReq req){
        if (loginUtils.getUser().getRoles().equals("hospital")){
            req.setHospitalUuid(loginUtils.getUser().getUserUuid());
        }
        PageData<DepartmentRsp> result = hospitalBiz.departmentList(req);
        return BaseResponse.success(result);
    };

    @ApiOperation(value = "根据id编辑部门配置")
    @RequestMapping(path = "/department/edit",method = RequestMethod.POST)
    @TokenVerify(type = "hospital")
    public BaseResponse editDepartment(@RequestBody DepartmentReq req){
        req.setHospitalUuid(loginUtils.getUser().getUserUuid());
        hospitalBiz.editDepartment(req);
        return BaseResponse.success();
    };

    @ApiOperation(value = "编辑部门配置前检查")
    @RequestMapping(path = "/department/edit-check",method = RequestMethod.POST)
    @TokenVerify(type = "hospital")
    public BaseResponse editDepartmentCheck(@RequestBody DepartmentReq req){
        req.setHospitalUuid(loginUtils.getUser().getUserUuid());
        Boolean flag = hospitalBiz.editDepartmentCheck(req);
        return BaseResponse.success(flag);
    };

    @ApiOperation(value = "删除部门配置")
    @RequestMapping(path = "/department/delete",method = RequestMethod.POST)
    @TokenVerify(type = "hospital")
    public BaseResponse departmentDelete(@RequestParam Long id){
        hospitalBiz.departmentDelete(id);
        return BaseResponse.success();
    };

    @ApiOperation(value = "添加医生")
    @RequestMapping(path = "/doctor/add",method = RequestMethod.POST)
    @TokenVerify(type = "hospital")
    public BaseResponse doctorAdd(@RequestBody DoctorAddReq req){
        req.setHospitalUuid(loginUtils.getUser().getUserUuid());
        hospitalBiz.doctorAdd(req);
        return BaseResponse.success();
    };

    @ApiOperation(value = "修改医生")
    @RequestMapping(path = "/doctor/update",method = RequestMethod.POST)
    @TokenVerify(type = "hospital")
    public BaseResponse doctorUpdate(@RequestBody DoctorAddReq req){
        req.setHospitalUuid(loginUtils.getUser().getUserUuid());
        hospitalBiz.doctorUpdate(req);
        return BaseResponse.success();
    };

    @ApiOperation(value = "查看医生")
    @RequestMapping(path = "/doctor/show",method = RequestMethod.POST)
    @TokenVerify(type = "hospital")
    public BaseResponse doctorShow(@RequestBody DoctorAddReq req){
        req.setHospitalUuid(loginUtils.getUser().getUserUuid());
        DoctorShowRsp result = hospitalBiz.doctorShow(req);
        return BaseResponse.success(result);
    };

    @ApiOperation(value = "更改医生状态")
    @RequestMapping(path = "/doctor/change-status",method = RequestMethod.POST)
    @TokenVerify(type = "hospital,doctor")
    public BaseResponse changeStatus(@RequestBody DoctorGetReq req){
        if (loginUtils.getUser().getRoles().equals("hospital")) {
            req.setHospitalUuid(loginUtils.getUser().getUserUuid());
        } else if (loginUtils.getUser().getRoles().equals("doctor")) {
            req.setDoctorUuid(loginUtils.getUser().getUserUuid());
        }

        hospitalBiz.changeStatus(req);
        return BaseResponse.success();
    };

    @ApiOperation(value = "审批医生介绍")
    @RequestMapping(path = "/doctor/introduce",method = RequestMethod.POST)
    @TokenVerify(type = "hospital")
    public BaseResponse doctorIntroduce(@RequestBody DoctorIntroduceReq req){
        req.setHospitalUuid(loginUtils.getUser().getUserUuid());
        hospitalBiz.doctorIntroduce(req);
        return BaseResponse.success();
    };

    @ApiOperation(value = "根据医生uuid获取医生信息")
    @RequestMapping(path = "/doctor/get",method = RequestMethod.POST)
    @TokenVerify(type = "hospital,doctor")
    public BaseResponse doctorGet(@RequestBody DoctorGetReq req){
        if (loginUtils.getUser().getRoles().equals("hospital")) {
            req.setHospitalUuid(loginUtils.getUser().getUserUuid());
        } else if (loginUtils.getUser().getRoles().equals("doctor")){
            req.setDoctorUuid(loginUtils.getUser().getUserUuid());
        }
        DoctorRsp doctorRsp = hospitalBiz.doctorGet(req);
        return BaseResponse.success(doctorRsp);
    };

    @ApiOperation(value = "查找医生列表")
    @RequestMapping(path = "/doctor/list",method = RequestMethod.POST)
    @TokenVerify(type = "hospital,user")
    public BaseResponse<PageData<DoctorListRsp>> doctorList(@RequestBody DoctorListReq req){
        if (loginUtils.getUser().getRoles().equals("hospital")){
            req.setHospitalUuid(loginUtils.getUser().getUserUuid());
        }
        PageData<DoctorListRsp> result = hospitalBiz.doctorList(req);
        return BaseResponse.success(result);
    };

    @ApiOperation(value = "查找被删除医生列表")
    @RequestMapping(path = "/doctor/delete-list",method = RequestMethod.POST)
    @TokenVerify(type = "hospital,user")
    public BaseResponse<PageData<DoctorDeleteRsp>> doctorDeleteList(@RequestBody DoctorDeleteListReq req){
        if (loginUtils.getUser().getRoles().equals("hospital")){
            req.setHospitalUuid(loginUtils.getUser().getUserUuid());
        }
        PageData<DoctorDeleteRsp> result = hospitalBiz.doctorDeleteList(req);
        return BaseResponse.success(result);
    };

    @ApiOperation(value = "设置医生下一周工作时间 定时任务开关")
    @RequestMapping(path = "/doctor/set-work-time",method = RequestMethod.POST)
    @TokenVerify(type = "hospital")
    public BaseResponse setWorkTime(@RequestBody DoctorOrderDetailReq req){
        req.setHospitalUuid(loginUtils.getUser().getUserUuid());
        if (req.getStatus() == 2) {
            hospitalBiz.setWorkTime(req);
        } else if (req.getStatus() == 1) {
            hospitalBiz.stopTask(req.getHospitalUuid());
        }
        return BaseResponse.success();
    };

    @ApiOperation(value = "获取定时任务信息")
    @RequestMapping(path = "/task/get",method = RequestMethod.POST)
    @TokenVerify(type = "hospital")
    public BaseResponse getTaskMessage(){
        String hospitalUuid = loginUtils.getUser().getUserUuid();
        QuartzCronRsp quartzCronRsp = hospitalBiz.getTaskMessage(hospitalUuid);
        return BaseResponse.success(quartzCronRsp);
    };

    @ApiOperation(value = "医院更新")
    @RequestMapping(path = "/update-message",method = RequestMethod.POST)
    @TokenVerify(type = "hospital")
    public BaseResponse updateIntroduce(@RequestBody HospitalUpdateIntroduceReq req){
        req.setHospitalUuid(loginUtils.getUser().getUserUuid());
        hospitalBiz.updateIntroduce(req);
        return BaseResponse.success();
    };

    @ApiOperation(value = "获取医院属性/简介")
    @RequestMapping(path = "/detail-message",method = RequestMethod.POST)
    @TokenVerify(type = "hospital,doctor,medicine,frontDesk")
    public BaseResponse getDetailMessage(@RequestBody HospitalMessageReq req){
        if (loginUtils.getUser().getRoles().equals("hospital")) {
            req.setHospitalUuid(loginUtils.getUser().getUserUuid());
        }
        HospitalMessageRsp hospitalMessageRsp = hospitalBiz.getDetailMessage(req);
        return BaseResponse.success(hospitalMessageRsp);
    };
}
